package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.PositionSmallTagCompanyRecruitmentMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.PositionSmallTagCompanyRecruitment;
import edu.hubu.talentmanagementsystem.service.IPositionSmallTagCompanyRecruitmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class PositionSmallTagCompanyRecruitmentServiceImpl extends ServiceImpl<PositionSmallTagCompanyRecruitmentMapper, PositionSmallTagCompanyRecruitment> implements IPositionSmallTagCompanyRecruitmentService {

    private final MapperContext context;

    public PositionSmallTagCompanyRecruitmentServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getPositionSmallTagCompanyRecruitmentMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<PositionSmallTagCompanyRecruitment> insert(@EncodeParam PositionSmallTagCompanyRecruitment record) {
        int insert = context.getPositionSmallTagCompanyRecruitmentMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getPositionSmallTagCompanyRecruitmentMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<PositionSmallTagCompanyRecruitment> selectById(Integer id) {
        QueryWrapper<PositionSmallTagCompanyRecruitment> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(PositionSmallTagCompanyRecruitment::getPositionSmallTagCompanyRecruitmentId, id);
        return Optional.of(context.getPositionSmallTagCompanyRecruitmentMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<PositionSmallTagCompanyRecruitment> selectAll(String order) {
        QueryWrapper<PositionSmallTagCompanyRecruitment> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> select = getColumnWrapper(wrapper.lambda());
            return context.getPositionSmallTagCompanyRecruitmentMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> select = getColumnWrapper(wrapper.lambda());
        return context.getPositionSmallTagCompanyRecruitmentMapper().selectList(select);
    }

    @Override
    public Page<PositionSmallTagCompanyRecruitment> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<PositionSmallTagCompanyRecruitment> wrapper = new QueryWrapper<>();
        Page<PositionSmallTagCompanyRecruitment> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> select = getColumnWrapper(wrapper.lambda());
            return context.getPositionSmallTagCompanyRecruitmentMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> select = getColumnWrapper(wrapper.lambda());
        return context.getPositionSmallTagCompanyRecruitmentMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> getColumnWrapper(LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> wrapper) {
        return wrapper.select(
                PositionSmallTagCompanyRecruitment::getPositionSmallTagId
                    ,
                PositionSmallTagCompanyRecruitment::getPositionSmallTag
                    ,
                PositionSmallTagCompanyRecruitment::getCompanyRecruitment
                    ,
                PositionSmallTagCompanyRecruitment::getPositionSmallTagCompanyRecruitmentId
                    ,
                PositionSmallTagCompanyRecruitment::getCompanyRecruitmentId
        );
    }

    @Override
    public Page<PositionSmallTagCompanyRecruitment> selectLikePaged( Integer currentPage,
             Integer pageSize, PositionSmallTagCompanyRecruitment record, String order) {
        Page<PositionSmallTagCompanyRecruitment> page = new Page<>(currentPage, pageSize);
        QueryWrapper<PositionSmallTagCompanyRecruitment> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getPositionSmallTagCompanyRecruitmentMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getPositionSmallTagCompanyRecruitmentMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> getQueryWrapper(QueryWrapper<PositionSmallTagCompanyRecruitment>wrapper, PositionSmallTagCompanyRecruitment record) {
        LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> lambda = wrapper.lambda();
        if (record.getPositionSmallTagId() != null) {
            lambda = lambda.eq(PositionSmallTagCompanyRecruitment::getPositionSmallTagId,record.getPositionSmallTagId());
        }
        if (record.getPositionSmallTag() != null) {
            lambda = lambda.eq(PositionSmallTagCompanyRecruitment::getPositionSmallTag,record.getPositionSmallTag());
        }
        if (record.getCompanyRecruitment() != null) {
            lambda = lambda.eq(PositionSmallTagCompanyRecruitment::getCompanyRecruitment,record.getCompanyRecruitment());
        }
        if (record.getPositionSmallTagCompanyRecruitmentId() != null) {
            lambda = lambda.eq(PositionSmallTagCompanyRecruitment::getPositionSmallTagCompanyRecruitmentId,record.getPositionSmallTagCompanyRecruitmentId());
        }
        if (record.getCompanyRecruitmentId() != null) {
            lambda = lambda.eq(PositionSmallTagCompanyRecruitment::getCompanyRecruitmentId,record.getCompanyRecruitmentId());
        }
        return lambda;
    }

    public int count(PositionSmallTagCompanyRecruitment record) {
        return context.getPositionSmallTagCompanyRecruitmentMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<PositionSmallTagCompanyRecruitment> updateById(Integer id, @EncodeParam PositionSmallTagCompanyRecruitment record) {
        record.setPositionSmallTagCompanyRecruitmentId(id);
        int update = context.getPositionSmallTagCompanyRecruitmentMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<PositionSmallTagCompanyRecruitment> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<PositionSmallTagCompanyRecruitment> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(PositionSmallTagCompanyRecruitment::getPositionSmallTagCompanyRecruitmentId, id);
            return Optional.of(context.getPositionSmallTagCompanyRecruitmentMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
